package com.projectzqjz.parttimework.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.projectzqjz.parttimework.App;
import com.projectzqjz.parttimework.R;
import com.projectzqjz.parttimework.base.BaseFragment;
import com.projectzqjz.parttimework.ui.activity.PartApplyActivity;
import com.projectzqjz.parttimework.ui.activity.PartFeedBackActivity;
import com.projectzqjz.parttimework.ui.activity.PartLoginActivity;
import com.projectzqjz.parttimework.ui.activity.PartMyCollectionActivity;
import com.projectzqjz.parttimework.ui.activity.PartMyDiaryActivity;
import com.projectzqjz.parttimework.ui.activity.PartSettingActivity;
import com.projectzqjz.parttimework.ui.activity.PartWebCommonActivity;
import com.projectzqjz.parttimework.utils.AppConfig;
import com.projectzqjz.parttimework.utils.NewsToastUtils;
import com.projectzqjz.parttimework.utils.ScreenUtil;
import com.projectzqjz.parttimework.utils.UserUtils;
import com.squareup.picasso.Picasso;
import com.tencent.connect.UserInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class PartMineFragment extends BaseFragment {
    @BindView(R.id.view_status_bar_place)
    View statusBarPlace;
    private boolean isFirst = true;
    @BindView(R.id.iv_img)
    ImageView iv_img;
    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_toLogin)
    TextView tv_toLogin;

    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String str) {
        //获取出价记录  然后进行设置
        try {
            //重新刷数据
            if ("checkLogin".equals(str)) {//检测是否登录过了
                if (UserUtils.checkLogin()) {
                    tv_toLogin.setVisibility(View.GONE);
                } else {
                    tv_toLogin.setVisibility(View.VISIBLE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public View setContentView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_part_mine, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        ViewGroup.LayoutParams params = statusBarPlace.getLayoutParams();
        params.height = ScreenUtil.getStatusBarHeight(getContext());
        statusBarPlace.setLayoutParams(params);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (UserUtils.checkLogin()) {
                tv_toLogin.setVisibility(View.GONE);
            } else {
                tv_toLogin.setVisibility(View.VISIBLE);
            }
            if (isFirst) {
                isFirst = false;
                Picasso.with(getActivity()).load(AppConfig.getInstance(App.getContext()).get("img_person_banner")).into(iv_img);
            }
        }
    }

    @OnClick({R.id.rel_collect, R.id.rel_cooperation, R.id.rel_feedBack, R.id.rel_setting
            , R.id.ll_apply, R.id.ll_all, R.id.ll_done, R.id.ll_continue, R.id.perfectResume, R.id.tv_toLogin
    })
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.ll_apply://已报名
                startActivity(new Intent(getActivity(), PartApplyActivity.class).putExtra("type", -1));
                break;
            case R.id.ll_all://全部
                startActivity(new Intent(getActivity(), PartApplyActivity.class).putExtra("type", -2));
                break;
            case R.id.ll_done://已完成
                startActivity(new Intent(getActivity(), PartApplyActivity.class).putExtra("type", 1));
                break;
            case R.id.ll_continue://进行中
                startActivity(new Intent(getActivity(), PartApplyActivity.class).putExtra("type", 0));
                break;
            case R.id.rel_collect://我的收藏
                startActivity(new Intent(getActivity(), PartMyCollectionActivity.class));
                break;
            case R.id.rel_cooperation://商务合作
                startActivity(new Intent(getActivity(), PartWebCommonActivity.class)
                        .putExtra("url", AppConfig.getInstance(App.getContext()).get("business_coop"))
                        .putExtra("title", "商务合作"));
                break;
            case R.id.rel_feedBack://我的反馈
                startActivity(new Intent(getActivity(), PartFeedBackActivity.class));
                break;
            case R.id.rel_setting://设置
                startActivity(new Intent(getActivity(), PartSettingActivity.class));
                break;
            case R.id.perfectResume://完善简历
                startActivity(new Intent(getActivity(), PartMyDiaryActivity.class));
                break;
            case R.id.tv_toLogin://直接去登录
                startActivity(new Intent(getActivity(), PartLoginActivity.class));
                break;
        }

    }
}
